package Analysis;

public class CorrelationCalculatorClass {
	
	
	public static double calculatePearsonCorrelation(double[] scores1,double[] scores2)
	{
		double result = 0;
		double sum_sq_x = 0;
		double sum_sq_y = 0;
		double sum_coproduct = 0;
		
		if(scores1==null || scores2==null)
			return Double.NaN;		
		
		double mean_x = scores1[0];
		double mean_y = scores2[0];
		for(int i=2;i<scores1.length+1;i+=1){
			double sweep =Double.valueOf(i-1)/i;
			double delta_x = scores1[i-1]-mean_x;
			double delta_y = scores2[i-1]-mean_y;
			sum_sq_x += delta_x * delta_x * sweep;
			sum_sq_y += delta_y * delta_y * sweep;
			sum_coproduct += delta_x * delta_y * sweep;
			mean_x += delta_x / i;
			mean_y += delta_y / i;
		}
		double pop_sd_x = (double) Math.sqrt(sum_sq_x/scores1.length);
		double pop_sd_y = (double) Math.sqrt(sum_sq_y/scores1.length);
		double cov_x_y = sum_coproduct / scores1.length;
		result = cov_x_y / (pop_sd_x*pop_sd_y);
		return result;
	}

	public static double calculateStatisticalCorrelation (double vector1[], double vector2[]) 
	{
		double expectedValueOfVector1 = Utility.WorkerUtilityClass.getAverage(vector1);
		double expectedValueOfVector2 = Utility.WorkerUtilityClass.getAverage(vector2);


		double[] multipliedResultOfTheVectors = Utility.WorkerUtilityClass.oneToOneMultiplyOfTwoVectors(vector1, vector2);
		double expectedValueOfmMultipliedResultOfTheVectors = Utility.WorkerUtilityClass.getAverage(multipliedResultOfTheVectors);

		double statisticalCorrelation;

		statisticalCorrelation = expectedValueOfmMultipliedResultOfTheVectors - expectedValueOfVector1*expectedValueOfVector2;

		return statisticalCorrelation;	    
	}

	public static double calculateStatisticalYildizCorrelation (double vector1[], double vector2[]) 
	{
		int numberOfSamples=vector1.length;
		double nominator, denominator;

		double[] multipliedResultOfTheVectors = Utility.WorkerUtilityClass.oneToOneMultiplyOfTwoVectors(vector1, vector2);


		double sumOfTheMultipliedVector = Utility.WorkerUtilityClass.getSumOfTheElementsOfTheVector(multipliedResultOfTheVectors);
		double sumOfFirstVector = Utility.WorkerUtilityClass.getSumOfTheElementsOfTheVector(vector1);
		double sumOfSecondVector = Utility.WorkerUtilityClass.getSumOfTheElementsOfTheVector(vector2);


		double firstTermInDenominator = Math.sqrt(numberOfSamples*Utility.WorkerUtilityClass.getSumOfTheElementsOfTheVector(Utility.WorkerUtilityClass.getSquaresOfTheVector(vector1))-Utility.WorkerUtilityClass.getSumOfTheElementsOfTheVector(vector1)*Utility.WorkerUtilityClass.getSumOfTheElementsOfTheVector(vector1));
		double secondTermInDenominator = Math.sqrt(numberOfSamples*Utility.WorkerUtilityClass.getSumOfTheElementsOfTheVector(Utility.WorkerUtilityClass.getSquaresOfTheVector(vector2))-Utility.WorkerUtilityClass.getSumOfTheElementsOfTheVector(vector2)*Utility.WorkerUtilityClass.getSumOfTheElementsOfTheVector(vector2));

		nominator = numberOfSamples*sumOfTheMultipliedVector-sumOfFirstVector*sumOfSecondVector;
		denominator = firstTermInDenominator * secondTermInDenominator;
		
		if(denominator==0)
			return -2;			

		return nominator/denominator;
	}
}
